#include <mutex>
#include <map>
#include <thread>
#include <queue>
#include "CStrCommandCenter.h"
#include "clcomcommand.h"

static mutex s_lock;
static CStrCommandCenter* s_instance = nullptr;

void IStrCommandCenter::Init()
{
	lock_guard<mutex> lock(s_lock);
	if (!s_instance)
	{
		s_instance = new CStrCommandCenter;
	}
}
void IStrCommandCenter::Release()
{
	lock_guard<mutex> lock(s_lock);
	if (!s_instance)
	{
		return;
	}
	delete s_instance;
	s_instance = nullptr;
}

void IStrCommandCenter::Subscribe(
	string strcmd,
	CStrCommandObserver* pob)
{
	Init();
	lock_guard<mutex> lock(s_lock);
	s_instance->Subscribe(strcmd, pob);
}

void IStrCommandCenter::Cancel(
	string strcmd,
	CStrCommandObserver* pob)
{
	lock_guard<mutex> lock(s_lock);
	if (!s_instance)
	{
		return;
	}
	s_instance->Cancel(strcmd, pob);
}

void IStrCommandCenter::Cancel(
	CStrCommandObserver* pob)
{
	lock_guard<mutex> lock(s_lock);
	if (!s_instance)
	{
		return;
	}
	s_instance->Cancel(pob);
}

qint64 IStrCommandCenter::PostCommand(
    CLComCommand& command,
	bool bNeedResult)
{
	lock_guard<mutex> lock(s_lock);
	if (!s_instance)
	{
		return 0;
	}
	return s_instance->PostCommand(command, bNeedResult);
}

bool IStrCommandCenter::GetCommandResult(
    qint64 id,
    CLComCommand& command)
{
	lock_guard<mutex> lock(s_lock);
	if (!s_instance)
	{
		return false;
	}
	return s_instance->GetCommandResult(id, command);
}
